package Config

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
)

type MyConfig struct {
	Url string `json:"url"`
}

func ConfigInit() (MyConfig, bool) {
	var conf MyConfig
	bpath := GetBasePath() + "/config.json"
	conf, bl := LoadConfig(bpath) //get config struct
	if !bl {
		return conf, false
	}
	//logger.Println("返回结果：",conf)

	return conf, true
}

func LoadConfig(filename string) (MyConfig, bool) {
	var conf MyConfig
	data, err := ioutil.ReadFile(filename) //read config file
	if err != nil {
		return conf, false
	}
	datajsond := []byte(data)
	err = json.Unmarshal(datajsond, &conf)
	if err != nil {
		return conf, false
	}
	return conf, true
}

//获取系统执行的目录
func GetBasePath() string {
	dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
	if err != nil {
		fmt.Printf("获取配置文件路径错误，%s", err)
	}
	return dir
}
